// Author: wsfuyibing <682805@qq.com>
// Date: 2025-04-28

package services

import (
	"context"
	"gitee.com/go-libs/db-xorm/db"
	"server/app/http/models"
	"server/app/kind"
)

func (o *UserLoginsService) ListLatest(ctx context.Context, uid kind.UserId) (list []*models.UserLogins, err error) {
	var sess *db.Session

	if sess, err = o.Dao.Slaver(ctx); err != nil {
		return
	}

	err = sess.Where("`user_id` = ?", uid).OrderBy("created_at DESC").Limit(10).Find(&list)
	return
}

func (o *UserLoginsService) Paging(ctx context.Context, uid kind.UserId, page, size int) (list []*models.UserLogins, total int64, err error) {
	var sess *db.Session
	if sess, err = o.Dao.Slaver(ctx); err == nil {
		if total, err = sess.Where("`user_id` = ?", uid).Count(models.UserLogins{}); err == nil && total > 0 {
			err = sess.Where("`user_id` = ?", uid).OrderBy("created_at DESC").Limit(size, (page-1)*size).Find(&list)
		}
	}
	return
}
